# Requires
require 'aasm_util'
require 'date_util'
require 'validators'

# WillPaginate I18n
# FIXME I18n ainda n ta carregado neste ponto (require i18n ?)
WillPaginate::ViewHelpers.pagination_options[:previous_label] = "&laquo;" # I18n.t("pagination.prev")
WillPaginate::ViewHelpers.pagination_options[:next_label]     = "&raquo;" # I18n.t("pagination.next")

# Constante APP_CONFIG com configurações globais específicas da aplicação
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]

# reCAPTCHA
ENV['RECAPTCHA_PUBLIC_KEY']  = APP_CONFIG['recaptcha_public_key']
ENV['RECAPTCHA_PRIVATE_KEY'] = APP_CONFIG['recaptcha_private_key']

# Patch pra não incluir um maldito div nos campos com erro quebrando o html
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  msg = instance.error_message
  if html_tag =~ /<(input|textarea|select)[>]+class=/
    class_attribute = html_tag =~ /class=['"]/
    html_tag.insert(class_attribute + 7, "error ")
  elsif html_tag =~ /<(input|textarea|select)/
    first_whitespace = html_tag =~ /\s/
    html_tag[first_whitespace] = " class='error' "
  end
  html_tag
end

# Nokogiri instead of ReXML
ActiveSupport::XmlMini.backend='Nokogiri'

# i18n on form labels
module ActionView
  module Helpers
    class InstanceTag
      def to_label_tag(text = nil, options = {})
        options = options.stringify_keys
        name_and_id = options.dup
        add_default_name_and_id(name_and_id)
        options.delete("index")
        options["for"] ||= name_and_id["id"]
        if text.blank?
          content = method_name.humanize
          if object.class.respond_to?(:human_attribute_name)
            content = object.class.human_attribute_name(method_name)
          end
        else
          content = text.to_s
        end
        label_tag(name_and_id["id"], content, options)
      end
    end
  end
end
